import requests
import redis
import json

#selenium 无头浏览器  phant
url = 'https://s.taobao.com/search?q={}&p4ppushleft=5%2C48&s={}&ajax=true'
query = '手机'
# windows_name_list=['<','>','/','\\','|','"','*','?']
# pic_dir = 'E:\\taobao\\'
coon = redis.Redis(host='127.0.0.1',port=6379)

# def window_name_format(name):
#     for i in windows_name_list:
#         name=name.replace(i,'')
#     return name

def get_info(taobao_url):
    response=requests.get(url=taobao_url)

    # result = re.findall('jsonp743\((.*)\);',response.text)[0]

    # print(json.loads(result))
    result_dict = json.loads(response.text)
    taobao_info={}
    for item in result_dict['mods']['grid']['data']['spus']:
        print('title '+item['title'])
        print('pic_url '+item['pic_url'])
        print('price '+item['price'])
        print('month_sales '+item['month_sales'])
        print('------------------------------------------')
        # response_img=requests.get(url='http:'+item['pic_url'])
        # print(pic_dir+window_name_format(item['title'])+'.jpg')
        # with open(pic_dir+window_name_format(item['title'])+'.jpg','wb') as file_img:
        #     file_img.write(response_img.content)

        taobao_info['title']=item['title']
        # taobao_info['pic_local_url']=pic_dir+item['title']+'.jpg'
        taobao_info['pic_www_url']=item['pic_url']
        taobao_info['price']=item['price']
        taobao_info['month_sales']=item['month_sales']

        coon.rpush('taobao_info_new', taobao_info )


for i in range(1,10):
    get_info(taobao_url=url.format(query,i*48))
